#include <iostream>
#include <string>
using namespace std;

void print_type_sizeof();
void char_type();  // 字符类型
void string_type();  // 字符串类型
void array_type();  // 数组类
void point_type();  // 指针类型

void print_type_sizeof()
{
    cout << "int siezeof: " << sizeof(int) << endl;
    cout << "char sizeof: " << sizeof(char) << endl;
}

void char_type()
{
    char a = 'a';
    cout << "The variable a value is: " << a << endl;
    cout << "The variable a ascii code is: " << int(a) << endl;

    a = 97;
    cout << "Use acsii code segment result is: " << a << endl;
}

void string_type()
{
    // 1. C 风格字符串
    char c1[] = "This is C style string.";
    cout << "C style string: " << c1 << endl;
    
    // 2. C++ 风格字符串
    // C++ 风格字符串需要引入 string 头文件
    string c2 = "This is C++ style string.";
    cout << "C++ style string: " << c2 << endl;
}

void array_type()
{
    // 定义方式1
    int a1[5];
    a1[0]=1;
    a1[1]=2;
    a1[4]=5;
    cout << "array define method 1: "<< a1 << endl;
    for (int i = 0; i < 5; i++)
	{
		cout << a1[i] << endl;
	}

    // 定义方式2
    int a2[5] = {1,2,3,4,5};
    cout << "array define method 2: " << a2 << endl;
    for (int i = 0; i < 5; i++)
	{
		cout << a2[i] << endl;
	}
    
    // 定义方式3
    int a3[] = {1,2,3,4,5};
    cout << "array define method 3: " << a3 << endl;
    for (int i = 0; i < 5; i++)
	{
		cout << a3[i] << endl;
	}

    // 获取数组内存大小
    cout << "array a3 memery size is: " << sizeof(a3) << endl;

    // 通过指针操作数组
    int *p = a3;
    cout << "use pointer operate array a3. a3 addres is: " << a3 << ", p value addres is: " << p << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "the element " << i << " address is " << p << " value is " << *p << endl;
        p++;
    }
}

void point_type()
{
    // const 修饰指针
    int a = 10;
	int b = 10;

	//const修饰的是指针，指针指向可以改，指针指向的值不可以更改
	const int * p1 = &a; 
	p1 = &b; //正确
	//*p1 = 100;  报错
	

	//const修饰的是常量，指针指向不可以改，指针指向的值可以更改
	int * const p2 = &a;
	//p2 = &b; //错误
	*p2 = 100; //正确

    //const既修饰指针又修饰常量
	const int * const p3 = &a;
	//p3 = &b; //错误
	//*p3 = 100; //错误

    // 利用指针作函数参数，可以修改实参的值 
}

int main()
{
    print_type_sizeof();
    char_type();
    string_type();
    array_type();
    return 0;
}